home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / tls / tls036.1.Z / tls036.1 / usr / bin / tsum / tsum
Encoding:
Text File  |  1992-09-15  |  6.1 KB  |  247 lines

  1. :
  2. # @(#) tsum 25.2 92/06/19 
  3. #
  4. # Copyright (C) 1988-1992 The Santa Cruz Operation, Inc.,
  5. # This Module contains Proprietary Information of
  6. # The Santa Cruz Operation, and should be treated as Confidential.
  7. #
  8. # tsum: used for summing bundled tapes, should work on xenix or unix
  9. # this program is internal SCO bundle tape summing technology
  10. # WARNING: DO NOT use for summing standard tapes, these
  11. # should still be summed using: dd if=dev | sum -r
  12.  
  13. # mikeeb 6/12/92
  14. # 322 based system had problems with SCSI tape
  15. # marks so we will assume that we won't have 
  16. # products with more than 50 tape marks, to catch errors
  17. #
  18. # tonyj 18 Jun 1992
  19. # Add the -R option to specify how many records to read from the tape.
  20. # If the -R option is not used, reaching $COUNTLIMIT (50) records is regarded
  21. # as an error, probably caused by overrunning the end of the tape.
  22.  
  23. # These are the main types of tape devices
  24. # Move which ever one you want as the default into
  25. # the last position
  26. TAPEDEVICE=/dev/nrcdt0        # Standard CD device
  27. TAPEDEVICE=/dev/nrStp0        # Standard SCSI Tape device
  28. TAPEDEVICE=/dev/nrct0        # Standard tape device
  29.  
  30. COUNTLIMIT=50            # Maximum number of records to read
  31.                 # from the tape.  If the user has not
  32.                 # specified a value, regard reaching
  33.                 # COUNTLIMIT as an error, probably caused
  34.                 # by missing the end-of-tape marker.
  35. RFLAG=NO            # If 'YES', user has specified a countlimit
  36.                 # with the -R option; do not issue
  37.                 # an error message when the limit
  38.                 # is reached.
  39. BSFACTOR=5k            # Blocking factor
  40. TMPTSUM=/tmp/tsum$$
  41. TMPCOUNT=/tmp/count$$
  42. TMPFILE=/tmp/tsumtmp$$
  43. ERRORFILE=/tmp/error$$
  44.  
  45. tapereset () {
  46.     RESETDEV=$1
  47.     echo "Reseting Device $RESETDEV...\c"
  48.     sleep 1
  49.     case $RESETDEV in
  50.     *ct0)    
  51.         TAPEARG="-c"
  52.         RESETCNTRLDEV=/dev/xct0
  53.         ;;
  54.     *Stp0)    TAPEARG="-s"
  55.         RESETCNTRLDEV=/dev/xStp0
  56.         ;;
  57.     *Stp1)    TAPEARG="-s"
  58.         RESETCNTRLDEV=/dev/xStp1
  59.         ;;
  60.     *cdt0)    TAPEARG="-s"
  61.         RESETCNTRLDEV=/dev/xcdt0
  62.         ;;
  63.     *)    error "Unable to determine cntrl device"
  64.         return 1
  65.         ;;
  66.     esac
  67.      case `uname -r` in
  68.         2.*)    echo "rewinding..."
  69.         tape rewind $RESETCNTRLDEV || return 1
  70.                 ;;
  71.         *)      echo "reseting...\c"
  72.         tape $TAPEARG reset $RESETCNTRLDEV || return 1
  73.         sleep 1
  74.         echo "rewinding..."
  75.         tape $TAPEARG rewind $RESETCNTRLDEV || return 1
  76.         sleep 1
  77.                 ;;
  78.         esac
  79.     sleep 1
  80.     return 0
  81. }
  82.  
  83. tsum() {
  84.     # parse args, check needed vars
  85.     [ $# = 3 ] || {
  86.         echo "ERROR: incorrect arg count to $0"
  87.         usage
  88.         exit 1
  89.     }
  90.     [ "$COUNTLIMIT" ] || {
  91.         echo "ERROR: variable COUNTLIMIT must be set"
  92.         exit 1
  93.     }
  94.     TSUMDEVICE=$1
  95.     TSUMFILE=$2
  96.     TSUMCOUNTFILE=$3
  97.     RETURNCODE=0
  98.     COUNT=0
  99.  
  100.     # sum tape
  101.     [ -f $ERRORFILE ] && rm -f $ERRORFILE
  102.  
  103.     while dd if=$TSUMDEVICE bs=$BSFACTOR 2> $TMPFILE
  104.     do
  105.         # check input/output records
  106.         inrec=`head -1 $TMPFILE | awk '{print $1}'`
  107.         outrec=`tail -1 $TMPFILE | awk '{print $1}'`
  108.         [ "$inrec" = "0+0" -a "$outrec" = "0+0" ] && {
  109.             echo "1" >$ERRORFILE 2>&1
  110.             break
  111.         }
  112.  
  113.         # check count of records
  114.         COUNT=`expr $COUNT + 1`
  115.         echo $COUNT > $TSUMCOUNTFILE 2>&1
  116.             expr $COUNT = $COUNTLIMIT > /dev/null 2>&1 && {
  117.             if [ "$RFLAG" = "NO" ] ; then   # Overrun error...
  118.                 echo "2" >$ERRORFILE 2>&1
  119.             fi
  120.                         break
  121.         }
  122.     done | sum -r | sed 's/^\(.....\)/\1 /' > $TSUMFILE
  123.     rm -f $TMPFILE
  124.         [ -s $ERRORFILE ] && {        # Error output was produced.
  125.                 case `cat $ERRORFILE` in
  126.         1)
  127.                         echo "WARNING: multiple sequential blank records"
  128.                         echo "Assuming the end of the tape."
  129.                         echo "This may be tied to 322-324 SCSI"
  130.                         echo "tape driver problems."
  131.                         echo "The tsum may be inaccurate."
  132.             return 1 
  133.                     ;;
  134.                 2)
  135.                         echo "ERROR: problems have been encountered"
  136.                         echo "whith the $TAPEDEVICE device driver."
  137.                         echo "more than $COUNTLIMIT tape marks found"
  138.             return 2
  139.                     ;;
  140.         esac
  141.         }
  142.     return 0
  143. }
  144.  
  145. displaytsum() {
  146.     TSUMFILE=$1    
  147.     TCOUNTFILE=$2
  148.  
  149.     TSUM=`cat $TSUMFILE | awk '{print $1}'`
  150.     TVOLUME=`cat $TSUMFILE | awk '{print $2}'`
  151.     TCOUNT=`cat $TCOUNTFILE`
  152.     echo "tsum (tape sum) = $TSUM   volume (512 byte blocks) = $TVOLUME   tape records = $TCOUNT"
  153.     return 0
  154. }
  155.  
  156. usage() {
  157.     echo "usage: tsum [-u] [-R count] [device path]"
  158.     echo "       -R <count>  Read only the specified number of records"
  159.     echo "       -u          Display this usage message"
  160.     echo " "
  161.     echo "       current valid device paths:"
  162.     echo "       /dev/nrct0         Standard Tape"
  163.     echo "       /dev/nrStp0        SCSI Tape 0"
  164.     echo "       /dev/nrStp1        SCSI Tape 1"
  165.     echo "       /dev/nrcdt0        CD Rom"
  166.     echo "       /dev/nurStp0       8mm SCSI Tape 0"
  167.     echo "       /dev/nurStp1       8mm SCSI Tape 1"
  168.     echo " "
  169. }
  170.  
  171. validatedevice() {
  172.     DEVICE=`basename $1`
  173.     case $DEVICE in    
  174.     nrct0 | nrStp0 | nrStp1 | nurStp0 | nurStp1 | nrcdt0)    return 0 ;;
  175.     *)                            return 1 ;;
  176.     esac
  177. }
  178.  
  179. # Print an error message
  180. error() {
  181.     echo "\nError: $*" >&2
  182.     return $FAIL
  183. }
  184.  
  185.  
  186. # MAIN
  187. # parse args
  188.  
  189. while [ "$#" != 0 ] 
  190. do
  191.     case $1 in
  192.     -R)    RFLAG=YES
  193.         COUNTLIMIT=`expr "0$2" : '\([0-9]*\)'`    #Ensure it's numeric.
  194.         if [ -z "$COUNTLIMIT" ] ; then
  195.             error "bad record count with -R option: $2"
  196.             exit $FAIL
  197.         elif [ "$COUNTLIMIT" -le 0 ] ; then
  198.             error "bad record count with -R option: $2"
  199.             exit $FAIL
  200.         fi
  201.         shift ; shift ;;
  202.     -u|-\?)    usage ; exit $FAIL ;;
  203.     *)    if [ "$#" = 1 ] ; then
  204.             TAPEDEVICE=$1 
  205.         else
  206.             error "incorrect arguments: $*"
  207.             usage ; exit $FAIL 
  208.         fi
  209.         shift ;;
  210.     esac
  211. done
  212.  
  213. [ -r "$TAPEDEVICE" ] || {
  214.     echo "ERROR: tape device $TAPEDEVICE does not exist"
  215.     usage
  216.     exit 1
  217. }
  218.  
  219. validatedevice $TAPEDEVICE || {
  220.     echo "ERROR: tape device $TAPEDEVICE is not valid"
  221.     echo "Tape device must be of type no-rewind"
  222.     echo " "
  223.     usage
  224.     echo "Do you want to attempt summing anyway ? (y/n)\c"
  225.     read a
  226.     [ "$a" = "y" ] || {
  227.         exit 1
  228.     }
  229. }
  230.     
  231. tapereset $TAPEDEVICE || {
  232.     echo "Error: unable to access device"
  233.     exit 1
  234. }
  235.  
  236. echo "Summing bundle tape device $TAPEDEVICE..."
  237. tsum $TAPEDEVICE $TMPTSUM $TMPCOUNT || exit 1
  238. displaytsum $TMPTSUM $TMPCOUNT
  239. rm -f $TMPSUM $TMPCOUNT
  240.  
  241. tapereset $TAPEDEVICE || {
  242.     echo "Error: unable to access device"
  243.     exit 1
  244. }
  245.  
  246. exit 0
  247.